package com.hctforgreen.greenservice.examine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.hctforgreen.greenservice.ExamHistoryListActivity;
import com.hctforgreen.greenservice.ExamSearchActivity;
import com.hctforgreen.greenservice.MainActivityV2;
import com.hctforgreen.greenservice.NewStudentTrainActivity;
import com.hctforgreen.greenservice.ParentActivity;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.ExamPaperEntity;
import com.hctforgreen.greenservice.model.ExamSubEntity;
import com.hctforgreen.greenservice.service.HctApplication;
import com.hctforgreen.greenservice.utils.ContantsUtil;
import com.hctforgreen.greenservice.utils.HctConstants;

import java.text.DecimalFormat;
import java.util.List;

/**
 * 试卷提交后的"考试成绩"展示页面
 *
 * @author Tallen Zhai
 */
public class ResultsShowActivity extends ParentActivity {
    List<ExamSubEntity> listEntity;
    private Button mBtnHome;
    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.results_show_activity);
        Intent intent = getIntent();
        ExamPaperEntity paperEntity = (ExamPaperEntity) intent.getSerializableExtra(HctConstants.ON_ACTIVITY_KEY_PAPER_ENTITY);
        initSkinLayout();
        initTitleButtonBar();
        initWindow(paperEntity);
        initShowBtnClick();
    }

    private void initTitleButtonBar() {
        final TextView titleTv = (TextView) findViewById(R.id.tv_title);
        titleTv.setText(getString(R.string.exam_result_title));
        //返回按钮，返回到搜索页面
        final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
        backBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                sendBroadcast(new Intent(ExamHistoryListActivity.FRESH_HISTORY_DATA_ACTION));
                sendBroadcast(new Intent(ExamSearchActivity.FRESH_EXAMINE_NUM_ACTION));
                sendBroadcast(new Intent(FINISH_EXAMINE_ACTIVITY_ACTION));
                finish();
            }
        });
        //回到首页或者资料页面
        mBtnHome = (Button) findViewById(R.id.btn_right);
        mIntent = new Intent();
        if (TextUtils.isEmpty(ContantsUtil.sStaffRole)) {
            mBtnHome.setText(getString(R.string.exam_right_button_main));
            mIntent.setClass(this, MainActivityV2.class);
        } else if (ContantsUtil.sStaffRole.equals(ContantsUtil.mRoles[0]) || ContantsUtil.sStaffRole.equals(ContantsUtil.mRoles[3])) {
            //家用安装跳转
            mBtnHome.setText(getString(R.string.exam_right_button_paper));
            mIntent.setClass(this, NewStudentTrainActivity.class);
            mIntent.putExtra("studyTypeId", "");
            mIntent.putExtra("personId", "");
            String childtypeId2 = "";
            mIntent.putExtra("childtypeId", childtypeId2);
            mIntent.putExtra("searchKey", "");
            mIntent.putExtra("trainType", "0");
        } else if (ContantsUtil.sStaffRole.equals(ContantsUtil.mRoles[1]) || ContantsUtil.sStaffRole.equals(ContantsUtil.mRoles[4])) {
            //家用维修跳转
            mBtnHome.setText(getString(R.string.exam_right_button_paper));
            mIntent.setClass(this, NewStudentTrainActivity.class);
            mIntent.putExtra("studyTypeId", "");
            mIntent.putExtra("personId", "");
            String childtypeId3 = "";
            mIntent.putExtra("childtypeId", childtypeId3);
            mIntent.putExtra("searchKey", "");
            mIntent.putExtra("trainType", "1");
        } else if (ContantsUtil.sStaffRole.equals(ContantsUtil.mRoles[2]) || ContantsUtil.sStaffRole.equals(ContantsUtil.mRoles[5])) {
            //家用安装维修跳转
            mBtnHome.setText(getString(R.string.exam_right_button_paper));
            mIntent.setClass(this, NewStudentTrainActivity.class);
            mIntent.putExtra("studyTypeId", "");
            mIntent.putExtra("personId", "");
            String childtypeId4 = "";
            mIntent.putExtra("childtypeId", childtypeId4);
            mIntent.putExtra("searchKey", "");
            mIntent.putExtra("trainType", "2");
        } else {//其他三种身份，暂时不做处理
            mBtnHome.setText(getString(R.string.exam_right_button_main));
            mIntent.setClass(this, MainActivityV2.class);
        }
        mBtnHome.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mIntent) {
                    mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(mIntent);
                    finish();
                }
            }
        });
        // InitTitle.initTitle(this, R.drawable.btn_home, "考试成绩", 0,
        // new OnClickListener() {
        // @Override
        // public void onClick(View v) {
        // doBackHome();
        // }
        // }, null);
    }

    private void doBackHome() {
        Intent intent = new Intent(ResultsShowActivity.this, MainActivityV2.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    private void initWindow(ExamPaperEntity paperEntity) {
        listEntity = HctApplication.examSubEntities;
        if (listEntity == null) {
            return;
        }
        HctApplication.examSubEntities = null;
        /**
         * 分数
         */
        double iTotalScore = 0;
        int iRightNum = 0;
        int iWrongNum = 0;
        for (ExamSubEntity examEntity : listEntity) {
            iTotalScore += Double.valueOf(examEntity.inputAnswerScore);
            if (examEntity.inputAnswerResult.equals(HctConstants.INPUT_ANSWER_RESULT_ALL_RIGHT)) {
                iRightNum++;
            } else if (examEntity.inputAnswerResult.equals(HctConstants.INPUT_ANSWER_RESULT_HALF_RIGHT) ||
                    examEntity.inputAnswerResult.equals(HctConstants.INPUT_ANSWER_RESULT_ALL_WRONG)) {
                iWrongNum++;
            }
        }
        /**
         * 分数
         */
        final TextView scoreTv = (TextView) findViewById(R.id.tv_score);
        DecimalFormat decimalFormat = new DecimalFormat("0");
        scoreTv.setText(decimalFormat.format(iTotalScore));
        /**
         * 考试结果
         */
        final TextView tips = (TextView) findViewById(R.id.exam_tips);
        final ImageView imgResult = (ImageView) findViewById(R.id.img_exam_result);
        if (paperEntity.passScore <= iTotalScore) {
            tips.setText(getString(R.string.exam_tips_pass));
            imgResult.setImageResource(R.drawable.exam_result_pass);
        } else {
            tips.setText(getString(R.string.exam_tips_fail));
            imgResult.setImageResource(R.drawable.exam_result_fail);
        }

        /**
         * 正确数
         */
        final TextView rightNumTv = (TextView) findViewById(R.id.tv_right_num);
        rightNumTv.setText(getString(R.string.exam_right_num)
                + String.valueOf(iRightNum)
                + getString(R.string.exam_result_unit));
        /**
         * 答错数
         */
        final TextView wrongNumTv = (TextView) findViewById(R.id.tv_wrong_num);
        wrongNumTv.setText(getString(R.string.exam_wrong_num)
                + String.valueOf(iWrongNum)
                + getString(R.string.exam_result_unit));
        /**
         * 正确率
         */
        final TextView ratioTv = (TextView) findViewById(R.id.tv_ratio);
        double iRatio = ((double) iRightNum / (double) listEntity.size()) * 100;
        ratioTv.setText(getString(R.string.exam_right_percent)
                + String.valueOf(String.format("%.2f", iRatio)) + "%");
    }

    private void initShowBtnClick() {
        Button showSituationLstBtn = (Button) findViewById(R.id.btn_show_situation_lst);
        showSituationLstBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(ResultsShowActivity.this,
                        ShowSituationListActivity.class);
                HctApplication.examSubEntities = listEntity;
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        /**
         * 拦截物理back键
         */
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            return true;
        } else {
            return false;
        }
    }
}
